package com.btr.ivle.backgroud;

import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;

import com.btr.ivle.R;
import com.btr.ivle.data.IVLEToken;
import com.btr.ivle.helper.DatabaseModule;
import com.btr.ivle.helper.DatabaseWeblink;
import com.btr.ivle.helper.URLtoString;

public class GetWeblinkTask extends AsyncTask<String, Void, Void>{
	
	private Context context;
	
	public GetWeblinkTask(Context context){
		this.context = context;
	}

	protected Void doInBackground(String... texts) {
		String CourseCode = texts[0], CourseID = "";
		String ID, URL, Description, Order;

		DatabaseModule dbModule = new DatabaseModule(this.context,IVLEToken.getUserID(),null,context.getResources().getInteger(R.integer.DBVersion));
		String table = IVLEToken.getUserID(); 
		String[] columns = new  String[] {dbModule.colID};
		String selection = dbModule.colCode + "=?";
		String[] selectionArgs = new  String[] {CourseCode};
		Cursor c = dbModule.getReadableDatabase().query(table, columns, 
				selection, selectionArgs, null, null, null, null);
		if(c.getCount()==1){
			c.moveToFirst();
			CourseID = c.getString(0);
		}
		c.close();
		dbModule.getReadableDatabase().close();
		dbModule.close();
		
		try{
        	URL url = new URL("https://ivle.nus.edu.sg/api/Lapi.svc/Module_Weblinks?APIKey=" +
        			context.getString(R.string.IVLE_API_KEY) + "&AuthToken=" + IVLEToken.getToken() + "&CourseID="
        				+ CourseID);
        	
        	JSONObject jObject = new JSONObject(URLtoString.get(url));
	        JSONArray resultsArrary = jObject.getJSONArray("Results");
	        
	        DatabaseWeblink dbWeblink = new DatabaseWeblink(this.context,IVLEToken.getUserID()+"_Weblink",null,context.getResources().getInteger(R.integer.DBVersion));
	        table = IVLEToken.getUserID() + "_Weblink";
	        ContentValues values;
	        
	        for (int i = 0; i < resultsArrary.length(); i++) {
	        	ID = resultsArrary.getJSONObject(i).getString("ID").toString();
	        	URL = resultsArrary.getJSONObject(i).getString("URL").toString();
	        	Description = resultsArrary.getJSONObject(i).getString("Description").toString();
	        	Order = resultsArrary.getJSONObject(i).getString("Order").toString();
	        	
        		values = new  ContentValues();
	        	values.put(dbWeblink.ID, ID);
	        	values.put(dbWeblink.URL, URL);
	        	values.put(dbWeblink.Description, Description);
	        	values.put(dbWeblink.Order, Order);
	        	values.put(dbWeblink.CourseCode, CourseCode);
	        	dbWeblink.getReadableDatabase().replace(table, null, values);
	        	
			}
	        dbWeblink.getReadableDatabase().close();
	        dbWeblink.close();
  
        }catch(Exception ex){
	    }
		return null;
	}
	
	@SuppressWarnings("unchecked")
    protected void onPostExecute(Void result) {
		AsyncTaskCompleteListener<String> l = (AsyncTaskCompleteListener<String>) context;
    	l.onAsyncTaskComplete(""); 
    }
}
